using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Slot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler
{
    public Item Item=null;
    public int number = 0;
   
    public void OnPointerDown(PointerEventData eventData)
    {
     
        if (Input.GetMouseButtonDown(0))
        {
            if (transform.childCount != 0 && GameFacade.Instance.inventoryManager.PickedItem.gameObject.activeSelf)
            {
                if (Item.Id == GameFacade.Instance.inventoryManager.PickedItem.GetComponent<Slot>().Item.Id)
                {
                    if (number + GameFacade.Instance.inventoryManager.PickedItem.GetComponent<Slot>().number <= Item.Capacity)
                    {

                        number += GameFacade.Instance.inventoryManager.PickedItem.transform.GetComponent<Slot>().number;
                        transform.GetChild(0).GetComponent<ItemUI>().SetNumber(number);
                        GameFacade.Instance.inventoryManager.PickedItem.transform.GetComponent<Slot>().Item = null;
                        GameFacade.Instance.inventoryManager.PickedItem.transform.GetComponent<Slot>().number = 0;
                        Destroy(GameFacade.Instance.inventoryManager.PickedItem.transform.GetChild(0));
                        GameFacade.Instance.inventoryManager.PickedItem.gameObject.SetActive(false);
                    }
                    else
                    {

                        GameFacade.Instance.inventoryManager.PickedItem.transform.GetComponent<Slot>().number -= Item.Capacity - number;
                        number = Item.Capacity;
                        transform.GetChild(0).GetComponent<ItemUI>().SetNumber(number);
                        GameFacade.Instance.inventoryManager.PickedItem.GetChild(0).GetComponent<ItemUI>().
                            SetNumber(GameFacade.Instance.inventoryManager.PickedItem.transform.GetComponent<Slot>().number);
                    }
                }
                else
                {

                    int temp = number;
                    number = GameFacade.Instance.inventoryManager.PickedItem.transform.GetComponent<Slot>().number;
                    GameFacade.Instance.inventoryManager.PickedItem.transform.GetComponent<Slot>().number = temp;
                    Destroy(GameFacade.Instance.inventoryManager.PickedItem.transform.GetChild(0).gameObject);
                    Destroy(transform.GetChild(0).gameObject);
                    Item tempitem = Item;
                    StoreItem(GameFacade.Instance.inventoryManager.PickedItem.transform.GetComponent<Slot>().ReturnItem(), number);
                    GameFacade.Instance.inventoryManager.PickedItem.GetComponent<Slot>().StoreItem(tempitem, temp);
                }
                return;
            }
            if (transform.childCount == 0 && !GameFacade.Instance.inventoryManager.PickedItem.gameObject.activeSelf)
            {
                return;
            }
            if (transform.childCount != 0 && !GameFacade.Instance.inventoryManager.PickedItem.gameObject.activeSelf)
            {

                GameFacade.Instance.inventoryManager.PickedItem.GetComponent<Slot>().StoreItem(Item, number);
                Item = null;
                number = 0;
                GameFacade.Instance.inventoryManager.PickedItem.gameObject.SetActive(true);
                Destroy(transform.GetChild(0).gameObject);
                return;
            }
            if (transform.childCount == 0 && GameFacade.Instance.inventoryManager.PickedItem.gameObject.activeSelf)
            {
                StoreItem(GameFacade.Instance.inventoryManager.PickedItem.transform.GetComponent<Slot>().ReturnItem(),
                    GameFacade.Instance.inventoryManager.PickedItem.transform.GetComponent<Slot>().number);
                GameFacade.Instance.inventoryManager.PickedItem.transform.GetComponent<Slot>().Item = null;
                GameFacade.Instance.inventoryManager.PickedItem.transform.GetComponent<Slot>().number = 0;
                Destroy(GameFacade.Instance.inventoryManager.PickedItem.transform.GetChild(0).gameObject);
                GameFacade.Instance.inventoryManager.PickedItem.gameObject.SetActive(false);

                return;
            }
        }
        
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
       
        if (transform.childCount != 0)
        {
            if (!GameFacade.Instance.inventoryManager.PickedItem.gameObject.activeSelf)
            {
                GameFacade.Instance.inventoryManager.Panel.gameObject.SetActive(true);
                GameFacade.Instance.inventoryManager.Panel.GetComponent<ToolTip>().SetText(Item);
            }

        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
       
        GameFacade.Instance.inventoryManager.Panel.gameObject.SetActive(false);
    }

    public void StoreItem(Item item,int number)
    {
        GameObject ItemGameobject = GameFacade.Instance.assetsFactory.LoadPrefab("SlotPrefab");
        ItemGameobject.transform.position = transform.position;
        ItemGameobject.transform.SetParent(this.transform);
        ItemGameobject.transform.GetComponent<ItemUI>().SetUI(item);
        Item = item;
        this.number = number;

    }
    public Item ReturnItem()
    {
        return Item;
    }
    public void Des()
    {
        Destroy(transform.GetChild(0).gameObject);
    }
}
